import java.io.*;
import java.util.*;


public class searchDB {

	/**
	 * 
	 * Main Driver for Searching. 
	 * 1. Database name (source file's name) given as the sole argument. 
	 * 2. Prompts for query command in a loop, executes query and prompts again. 
	 * 3. Makes sure that input is sensible and gives intelligent errors.  
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length < 1){
			System.out.println("Error: Not Enough Arguments\nUSAGE: searchDB <collection_name>\n");
			System.exit(-1);
		}
		if(args.length > 1){
			System.out.println("Error: Too Many Arguments\nUSAGE: searchDB <collection_name>\n");
			System.exit(-1);
		}
		File collection = new File(args[0]);
		if(!collection.exists()){
			System.out.println("Error: "+args[0]+" does not exist as a file, collection missing.\nUSAGE: searchDB <collection_name>\n");
			System.exit(-1);
		}
		File dir = new File(collection.getPath()+"_Index");
		if(!dir.exists()){
			System.out.println("Error: "+args[0]+" has not been processed.\nPlease execute \"searchDB "+args[0]+"\" first.\n");
			System.exit(-1);
		}
		WordListManager.loadIndexFile(collection);
		boolean quit = false;  //setting to true quits the command loop and exits the program normally. 
		Scanner input = new Scanner(System.in); //loop persistent scanner of console input. 
		do {
			System.out.println();  
			System.out.print(collection.getName()+">");  //command prompt
			String line = input.nextLine();  //read the line to be executed.  
			Scanner scan_line = new Scanner(line); //scan the line for discrete arguments. 
			
			if(!scan_line.hasNext()){
				printUsage();
				continue;
			}
			String command = scan_line.next();
			command = command.toLowerCase();
			if(command.compareTo("quit") == 0){ //quit searching. 
				quit = true; //exit condition for loop
				WordListManager.writeIndexFile();
 				continue; 
			} else if (command.compareTo("query") == 0){ //search for single word. 
				if(!scan_line.hasNext()){
					System.out.println("Error: you must provide a word to be queried with this command");
					printUsage();
					continue;
				}
				String word = scan_line.next();
				if(!scan_line.hasNextInt()){
					System.out.println("Error: Expecting the desired number of results after word:"+word+"\n");
					printUsage();
					continue;
				}
				int numResults = scan_line.nextInt();
				if(scan_line.hasNext()){
					System.out.println("Error: you have provided too many arguments for this command to execute");
					printUsage();
					continue;
				}
				word = word.toLowerCase();
				System.out.println();
				System.out.println("======== RESULTS ===================");
				System.out.println(QueryDB.performQuery(collection, word, numResults));
				System.out.println("======== End Results ===============");
				System.out.println();
				continue;
			} else if (command.compareTo("mquery") == 0){ //search for instances of two words.   
				if(!scan_line.hasNext()){
					System.out.println("Error: you must provide two words to be queried with this command");
					printUsage();
					continue;
				}
				String word1 = scan_line.next();
				if(!scan_line.hasNext()){
					System.out.println("Error: you must provide two words to be queried with this command");
					printUsage();
					continue;
				}
				String word2 = scan_line.next();
				if(!scan_line.hasNextInt()){
					System.out.println("Error: Expecting the desired number of results after the second word:"+word2+"\n");
					printUsage();
					continue;
				}
				int numResults = scan_line.nextInt();
				if(scan_line.hasNext()){
					System.out.println("Error: you have provided too many words for this command to be queried");
					printUsage();
					continue;
				}
				System.out.println();
				System.out.println("======== RESULTS ===================");
				System.out.println(QueryDB.performMQuery(collection, word1, word2, numResults));
				System.out.println("======== End Results ===============");
				System.out.println();
				continue;
				
			} else { //unrecognized command. 
				System.out.println("command: \""+command +"\" is not recognized");
				printUsage();
			}
		} while(!quit);
		
	}

	public static void printUsage(){
		System.out.println();
		System.out.println("Valid commands are one of the following:");
		System.out.println("collection> query <word> <number> -- returns number (0 = all) results matching the word in the collection.");
		System.out.println("collection> mquery <first word> <second word> <number> -- returns number (0 = all) results matching both words.");
		System.out.println("collection> quit -- exits the program.");
		System.out.println();
	}
	
}
